package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.*;
import com.abaaba.project2.bean.bo.AdminCommentReplyBO;
import com.abaaba.project2.bean.bo.AdminDeleteCommentInformationBO;
import com.abaaba.project2.bean.vo.AdminGoodsCommentOfListVO;
import com.abaaba.project2.bean.vo.AdminGoodsDataSendingListVO;
import com.abaaba.project2.mapper.CommentMapper;
import com.abaaba.project2.mapper.GoodsMapper;
import com.abaaba.project2.service.AdminCommentService;
import com.abaaba.project2.util.DateToStringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminCommentServiceImpl implements AdminCommentService {


    @Autowired
    CommentMapper commentMapper;

    @Override
    public AdminGoodsCommentOfListVO commentSearchAllList(CommentDataSendingListBaseReq baseReq) {
        PageHelper.startPage(baseReq.getPage(),baseReq.getLimit());
        AdminGoodsCommentOfListVO adminGoodsCommentOfListVO = new AdminGoodsCommentOfListVO();
        CommentExample commentExample = new CommentExample();
        if (baseReq.getUserId()!=null){
            //Integer goodsSn = Integer.parseInt(baseReq.getGoodsSn());
            CommentExample.Criteria criteria = commentExample.createCriteria();
            /*.andNameEqualTo("%"+baseReq.getName()+"%")*/
            criteria.andUserIdEqualTo(baseReq.getUserId());
        }/*else if (baseReq.getGoodsSn()==null){
            GoodsExample.Criteria criteria = goodsExample.createCriteria();
            criteria.andNameEqualTo(baseReq.getName());
        }*/
        if (baseReq.getValueId()!=null){
            CommentExample.Criteria criteria = commentExample.createCriteria();
            criteria.andValueIdEqualTo(baseReq.getValueId());
        }

        /*显示数据*/
        AdminGoodsCommentOfListVO.DataBean dataBean = new AdminGoodsCommentOfListVO.DataBean();

        List<Comment> comments = commentMapper.selectByExample(commentExample);

        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        long total = commentPageInfo.getTotal();
        dataBean.setTotal((int)total);

        List<AdminGoodsCommentOfListVO.DataBean.ItemsBean> itemsBeans = new ArrayList<>();
        for (Comment comment : comments) {
            DateToStringUtil dateToStringUtil = new DateToStringUtil();
            String addTime =  dateToStringUtil.dateToString(comment.getAddTime());
            String updateTime = dateToStringUtil.dateToString(comment.getUpdateTime());
            AdminGoodsCommentOfListVO.DataBean.ItemsBean itemsBean = new AdminGoodsCommentOfListVO.DataBean.ItemsBean(comment.getId(),comment.getValueId(),comment.getType(),comment.getContent(),comment.getUserId(),comment.getHasPicture(),comment.getStar(),addTime,updateTime,comment.getDeleted(),comment.getPicUrls());
            itemsBeans.add(itemsBean);
        }
        dataBean.setItems(itemsBeans);

        adminGoodsCommentOfListVO.setData(dataBean);
        adminGoodsCommentOfListVO.setErrmsg("成功");
        adminGoodsCommentOfListVO.setErrno(0);
        return adminGoodsCommentOfListVO;
    }

    @Override
    public int deleteCommentInformation(AdminDeleteCommentInformationBO adcibo) {

        //Comment comment = new Comment(adcibo.getId(),adcibo.getValueId(),adcibo.getContent(),adcibo.getUserId(),adcibo.isDeleted());
        int code = commentMapper.deleteByPrimaryKey(adcibo.getId());
        if (code == 1){
            return 200;
        }
        return 404;
    }

    @Override
    public int CommentReply(AdminCommentReplyBO adminCommentReplyBO) {
        return 200;
    }
}
